<script setup lang="ts">
import haveHeadCard from "@/components/haveHead-card.vue";
import goodListEmit from "@/components/goodListEmit.vue"
import {ref} from 'vue'

const parentBorder = ref(false)
const childBorder = ref(false)
// 商品列表数据
const tableData = ref([
  {
    id: 14,
    name: '小米汽车',
    type: '电车',
    price: "21w",
    img: '//p3.dcarimg.com/img/tos-cn-i-dcdx/b4cef3f693ea4b87b1cdc642951d040b~1200x0.png',
    imgCount: 16,
    desc: '小米汽车是一款高端的汽车，拥有极高的动力和性能，是一款值得拥有的购买.',
    date: '2016-05-02',
    sales: 10000,
    evaluate: 100,
  },
  {
    id: 15,
    name: '特斯拉汽车',
    type: '电车',
    price: "21w",
    img: '//p3.dcarimg.com/img/tos-cn-i-dcdx/b63959a456e044098e6f40184b965f28~1200x0.png',
    imgCount: 24,
    desc: '特斯拉汽车是一款高端的汽车，拥有极高的动力和性能，是一款值得拥有的购买.',
    date: '2016-05-20',
    sales: 24500,
    evaluate: 98,
  },
  {
    id: 15,
    name: '奥迪A9',
    type: '油车',
    price: "201w",
    img: 'https://p3-dcd.byteimg.com/motor-mis-img/d47a55c1a930619104523b462a64fe78~tplv-f042mdwyw7-original:960:0.image',
    imgCount: 31,
    desc: '特斯拉汽车是一款高端的汽车，拥有极高的动力和性能，是一款值得拥有的购买.',
    date: '2016-04-15',
    sales: 1499,
    evaluate: 95,
  },
  {
    id: 17,
    name: '蔚来汽车',
    type: '电车',
    price: "33.8w",
    img: '//p3.dcarimg.com/img/tos-cn-i-dcdx/0daff1a31913447983bfbc292beb9d8b~1200x0.png',
    imgCount: 48,
    desc: '蔚来汽车是一款高端的汽车，拥有极高的动力和性能，是一款值得拥有的购买.',
    date: '2016-08-01',
    sales: 24500,
    evaluate: 94,
  },
])


//编辑弹窗数据
let editOrderData = ref({})
//编辑下标
let editIndex: any = ref(null)
//弹窗状态
let dialogVisible = ref(false)

//关闭弹窗
function closeMask(data: boolean) {
  dialogVisible.value = data
}

// 编辑商品
function editClick(row: object, index: number) {
  editOrderData.value = row
  dialogVisible.value = true
  editIndex.value = index
}

// 获取编辑表单数据
function getFormData(data: object) {
  tableData.value[editIndex.value] = data
}

//分页
//分页数据
const currentPage4 = ref(4)
const pageSize4 = ref(10)
const small = ref(false)
const background = ref(false)
const disabled = ref(false)

const handleSizeChange = (val: number) => {
  console.log(`${val} items per page`)
}
const handleCurrentChange = (val: number) => {
  console.log(`current page: ${val}`)
}

// 删除商品
function deleteClick(index: number) {
  tableData.value.splice(index, 1)
}


</script>

<template>
  <div class="goods-list">
    <haveHeadCard>
      <!-- 标题 -->
      <template #title>
        <h1>商品列表</h1>
      </template>
      <!-- 内容 -->
      <template #content>
        <el-table :data="tableData" :border="parentBorder" style="width: 100%">
          <el-table-column type="expand">
            <template #default="props">
              <div class="colum-content">
                <div m="4" class="colum-content-item">
                  <p m="t-0 b-2">商品ID: {{ props.row.id }}</p>
                  <p m="t-0 b-2">所属分类: {{ props.row.type }}</p>
                  <p m="t-0 b-2">商品图片: {{ props.row.imgCount }}</p>
                  <p m="t-0 b-2">商品评价: {{ props.row.evaluate }}</p>
                  <p m="t-0 b-2">商品描述: {{ props.row.desc }}</p>
                </div>
                <div m="4" class="colum-content-item">
                  <p m="t-0 b-2">商品名称: {{ props.row.name }}</p>
                  <p m="t-0 b-2">商品价格: {{ props.row.price }}</p>
                  <p m="t-0 b-2">创建时间: {{ props.row.date }}</p>
                  <p m="t-0 b-2">商品销量: {{ props.row.sales }}</p>
                </div>
              </div>
            </template>
          </el-table-column>
          <el-table-column label="商品名称" prop="name" width="200"/>
          <el-table-column label="所属分类" prop="type" width="200"/>
          <el-table-column label="商品价格" prop="price"
                           width="200"/>
          <el-table-column label="商品图片" width="200">
            <template #default="scope">
              <img :src="scope.row.img">
            </template>
          </el-table-column>
          >
          <el-table-column label="商品描述" prop="desc"/>
          <el-table-column fixed="right" label="操作">
            <template #default="scope">
              <el-button type="primary" size="large" @click="editClick(scope.row, scope.$index)">编辑</el-button>
              <el-button type="danger" size="large" @click="deleteClick(scope.$index)">
                删除
              </el-button>

            </template>
          </el-table-column>
        </el-table>
      </template>
      <!-- 页脚 -->
      <template #footer>
        <div class="footer">
          <el-pagination
              v-model:current-page="currentPage4"
              v-model:page-size="pageSize4"
              :small="small"
              :disabled="disabled"
              :background="background"
              layout="total,prev, pager, next, jumper"
              :total="400"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
          />
        </div>
      </template>
    </haveHeadCard>

    <goodListEmit :dialogVisible="dialogVisible" @close-mask="closeMask" :editOrderData="editOrderData"
                  @emit-form="getFormData"></goodListEmit>
  </div>
</template>

<style scoped>
.colum-content {
  display: flex;
  padding: 10px;
  box-sizing: border-box;
}

.colum-content-item {
  width: 300px;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 25px;

}

.colum-content-item p {
  width: 100%;
  overflow: hidden;
  /* 防止文本换行 */
  white-space: nowrap;
  /* 当文本溢出容器时，使用省略号表示 */
  text-overflow: ellipsis;
}

.colum-content-item:nth-child(2) {
  margin-left: 500px;
}

img {
  width: 100px;
  height: 100px;
}
</style>